package com.hckjsec.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.hckjsec.cloud.domain.AppletsApplicantUser;
import com.hckjsec.cloud.domain.AppletsApplicantUserLog;
import com.hckjsec.cloud.domain.wechatView.UserLogView;
import com.hckjsec.cloud.service.IAppletsApplicantUserLogService;
import com.hckjsec.cloud.service.IAppletsApplicantUserService;
import com.hckjsec.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.AppletsApplicantUser;
import com.hckjsec.cloud.service.IAppletsApplicantUserService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 微信用户人员信息Controller
 * 
 * @author cyt
 * @date 2021-09-07
 */
@RestController
@RequestMapping("/system/applicantUser")
public class AppletsApplicantUserController extends BaseController
{
    @Autowired
    private IAppletsApplicantUserService appletsApplicantUserService;

    @Autowired
    private IAppletsApplicantUserLogService appletsApplicantUserLogService;

    /**
     * 查询微信用户人员信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppletsApplicantUser appletsApplicantUser)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            appletsApplicantUser.setDeptId(SecurityUtils.getDeptId());
        }
        List<AppletsApplicantUser> list = appletsApplicantUserService.selectAppletsApplicantUserUrlList(appletsApplicantUser);
        return getDataTable(list);
    }

    /**
     * 导出微信用户人员信息列表
     */
    @Log(title = "微信用户人员信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppletsApplicantUser appletsApplicantUser)
    {
        List<AppletsApplicantUser> list = appletsApplicantUserService.selectAppletsApplicantUserUrlList(appletsApplicantUser);
        ExcelUtil<AppletsApplicantUser> util = new ExcelUtil<AppletsApplicantUser>(AppletsApplicantUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取微信用户人员信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        UserLogView userLogView = new UserLogView();
        List<AppletsApplicantUserLog> appletsApplicantUserLogList = new ArrayList<>();
        List<AppletsApplicantUser> appletsApplicantUserList = new ArrayList<>();
        AppletsApplicantUser appletsApplicantUser = appletsApplicantUserService.selectAppletsApplicantUserById(id);
        if ("TRUE".equals(appletsApplicantUser.getIsself())){
            appletsApplicantUserLogList = appletsApplicantUserLogService.selectAppletsApplicantUserLogList(new AppletsApplicantUserLog(appletsApplicantUser.getOpenid()));
        }
        AppletsApplicantUser applicantUser = new AppletsApplicantUser();
        applicantUser.setOpenid(appletsApplicantUser.getOpenid());
        List<AppletsApplicantUser> applicantUserList = appletsApplicantUserService.selectAppletsApplicantUserList(applicantUser);
        applicantUserList.forEach(kv->{
            if (kv.getId() != id){
                appletsApplicantUserList.add(kv);
            }
        });
        userLogView.setAppletsApplicantUserLogList(appletsApplicantUserLogList);
        userLogView.setAppletsApplicantUserList(appletsApplicantUserList);
        return AjaxResult.success(userLogView);
    }
}
